<?PHP
    require_once('ConnectionMysql.php');
    require_once('Comentario.php');

    class ComentarioDAO
    {
        private $connection = NULL;


        /*************************************************************************
        * Name: __construct
        * Description: Construtor da Classe. Inicializar os Atributos.
        * Parameters: 
        * Returns: Nenhum
        * Author: Felipe O. Simões
        * Last Modified: 00/00/00 - Felipe O. Simões
        *************************************************************************/
        public function __construct()
        {
            $this->connection = new ConnectionMysql(); // Cria a Conexão.
        }


        /*************************************************************************
        * Name: 
        * Description: 
        * Parameters: 
        * Returns: 
        * Author: Felipe O. Simões
        * Last Modified: 00/00/00 - Felipe O. Simões
        *************************************************************************/
        public function insert(Comentario $obj)
        {
            $sql = "INSERT INTO comentario (conteudo, data, idPostagem, idUsuario, isAtivo)
            VALUES ('". $obj->getConteudo() ."', '". $obj->getData() ."', ". $obj->getIdPostagem() .", ". $obj->getIdUsuario() .", '". $obj->getIsAtivo() ."')";
            
            $this->connection->queryConnect($sql);
        }


        /*************************************************************************
        * Name: 
        * Description: 
        * Parameters: 
        * Returns: 
        * Author: Felipe O. Simões
        * Last Modified: 00/00/00 - Felipe O. Simões
        *************************************************************************/
        public function deletePorId($id)
        {
            $sql = "UPDATE comentario SET isAtivo='N' WHERE idComentario=". $id;
            $this->connection->queryConnect($sql);
        }
        
        
        /*************************************************************************
        * Name: 
        * Description: 
        * Parameters: 
        * Returns: 
        * Author: Felipe O. Simões
        * Last Modified: 00/00/00 - Felipe O. Simões
        *************************************************************************/
        public function deletePorObj(Comentario $obj)
        {
            $sql = "UPDATE comentario SET isAtivo='N' WHERE idComentario=". $obj->getIdComentario();
            $this->connection->queryConnect($sql);
        }


        /*************************************************************************
        * Name: 
        * Description: 
        * Parameters: 
        * Returns: 
        * Author: Felipe O. Simões
        * Last Modified: 00/00/00 - Felipe O. Simões
        *************************************************************************/
        public function update(Comentario $obj)
        {
            $sql = "UPDATE comentario 
                    SET conteudo='". $obj->getConteudo() ."', data='". $obj->getData() ."', idPostagem=". $obj->getIdPostagem() .", idUsuario=". $obj->getIdUsuario() .", isAtivo='". $obj->getIsAtivo() ."' 
                    WHERE idComentario=". $obj->getIdComentario();
            
            $this->connection->queryConnect($sql);
        }


        /*************************************************************************
        * Name: 
        * Description: 
        * Parameters: 
        * Returns: 
        * Author: Felipe O. Simões
        * Last Modified: 00/00/00 - Felipe O. Simões
        *************************************************************************/
        public function selectPorPostagem($idPostagem)
        {
            $sql = "SELECT idComentario, conteudo, data, idUsuario FROM comentario 
                    WHERE isAtivo='S' AND idPostagem=". $idPostagem;
            $result = $this->connection->queryConnect($sql);
            
            if (mysql_num_rows($result) > 0)
            {
                $arrayObj = array();
                
                for ($i = 0; $i < mysql_num_rows($result); $i++)
                {
                    $arrayObj[$i] = new Comentario();
                    $arrayObj[$i]->setIdComentario(mysql_result($result, $i, "idComentario"));
                    $arrayObj[$i]->setConteudo(mysql_result($result, $i, "conteudo"));
                    $arrayObj[$i]->setData(mysql_result($result, $i, "data"));
                    $arrayObj[$i]->setIdUsuario(mysql_result($result, $i, "idUsuario"));
                }

                return $arrayObj;
            }
            else
            {
                return NULL;
            }
        }
        
        
        /*************************************************************************
        * Name: 
        * Description: 
        * Parameters: 
        * Returns: 
        * Author: Felipe O. Simões
        * Last Modified: 00/00/00 - Felipe O. Simões
        *************************************************************************/
        public function select()
        {
            $sql = "";
            $this->connection->queryConnect($sql);
        }
    }
?>